package com.xiaoshangkou.hellopra.controller.backstage;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaoshangkou.hellopra.mapper.UserGroupMapper;
import com.xiaoshangkou.hellopra.pojo.Member;
import com.xiaoshangkou.hellopra.pojo.UserGroup;
import com.xiaoshangkou.hellopra.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/backstage/member")
public class MemberController {
    @Autowired
    private MemberService memberService;
    @Autowired
    private UserGroupMapper userGroupMapper;

    /**
     * 用途：进入用户页面时分页查询所有用户
     * @RequestParam :设置默认值
     * page:当前页的页码（current）
     * size:每页显示的条数（size）
     */
    @RequestMapping("/all")
    public ModelAndView all(@RequestParam(defaultValue = "1")int page,
                            @RequestParam(defaultValue = "6")int size){
        ModelAndView modelAndView=new ModelAndView();
        Page<Member> memberPage = memberService.findPage(page, size);
        List<Member> records = memberPage.getRecords();//mybatisPlus默认getRecords出来的是全量数据(如果不需要那么要具体配置)

        for (Member member : records) {
            UserGroup userGroup = userGroupMapper.selectById(member.getUid());
            member.setUserGroup(userGroup);
        }

        modelAndView.addObject("memberPage",memberPage);

        modelAndView.setViewName("/backstage/member_all");
        return modelAndView;
    }

    /**
     * 用途:开启/禁用用户
     * mid:用户的mid
     */
    @RequestMapping("/updateStatus")
    public String updateStatus(Integer mid){
        memberService.updateStatus(mid);
        return "redirect:/backstage/member/all";
    }

    /**
     * 用途：根据用户名称搜索的功能
     * @RequestParam :设置默认值
     * page:当前页的页码（current）
     * size:每页显示的条数（size）
     * name:用户的名称
     * 要改善
     */
    @RequestMapping("/search")
    public ModelAndView findPageByName(@RequestParam(defaultValue = "1")int page,
                            @RequestParam(defaultValue = "6")int size,String name){
        ModelAndView modelAndView=new ModelAndView();
        Page<Member> memberPage = memberService.findPageByName(page, size,name);
        List<Member> records = memberPage.getRecords();//mybatisPlus默认getRecords出来的是全量数据(如果不需要那么要具体配置)

        //添加用户属于什么组别
        for (Member member : records) {
            UserGroup userGroup = userGroupMapper.selectById(member.getUid());
            System.out.println(userGroup.getUserGroupName());
            member.setUserGroup(userGroup);
        }

        modelAndView.addObject("memberPage",memberPage);

        modelAndView.setViewName("/backstage/member_search");
        return modelAndView;
    }

    /**
     * 用途:用户点击更改用户组，跳转到分配页显示所有用户组
     * 用户拥有的用户组，默认打上勾
     * @param mid 用户mid
     * mid传入member_usergroup是为了分配完角色后，提交到后端，后端知道是谁
     */
    @RequestMapping("/findUserGroup")
    public ModelAndView findUserGroup(Integer mid){
        ModelAndView modelAndView=new ModelAndView();
        Member member = memberService.findMember(mid);
        List<UserGroup> userGroups = memberService.findAll();
        modelAndView.addObject("member",member);
        modelAndView.addObject("userGroups",userGroups);
        modelAndView.addObject("mid",mid);
        modelAndView.setViewName("/backstage/member_usergroup");
        return modelAndView;

    }

    /**
     * 用途：用户重新分配用户组
     * @param ids 分配的新用户组
     * @param mid 需要分配用户组的用户
     * @return
     */
    @RequestMapping("/updateUserGroup")
    public String updateUserGroup(Integer ids,Integer mid){
        memberService.updateUserGroup(ids,mid);
        return "redirect:/backstage/member/all";
    }

}
